package com.xszx.knowledge.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xszx.knowledge.dto.PageReq;
import com.xszx.knowledge.entity.Category;
import com.xszx.knowledge.service.CategoryService;
import com.xszx.knowledge.util.Result;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 分类 前端控制器
 * </p>
 *
 * @author wencai
 * @since 2023/04/24 15:11
 */
@Api(tags = "分类管理")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    /**
     * 查询全部的数据
     * @return
     */
    @GetMapping("/getAll")
    public Result getAllCategory(){
        return Result.ok(categoryService.list());
    }

    /**
     * 进行数据分页的查询
     * @return
     */
    @GetMapping("/getPage")
    public Result getPageCategory(PageReq pageReq){
        //指定当前页和每页显示数据的条数
        Page page = new Page(pageReq.getCurrent(),pageReq.getSize());
        //进行数据的查询
        IPage<Category> categoryIPage = categoryService.page(page);
        return Result.ok(categoryIPage);
    }

    /**
     * 定义添加或者更新的接口
     */
    @PostMapping("/save")
    public Result save(@RequestBody Category category){
        Result reuslt =Result.ifSuccess(categoryService.saveOrUpdate(category));
        return reuslt;
    }
    /**
     * 定义删除接口
     */
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable long id){
        return Result.ifSuccess(categoryService.removeById(id));
    }
}
